﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.IO;
using System.Configuration;
using System.Text.RegularExpressions;
using AnyOffice.DAL;
using AnyOffice.Components;
using AnyOffice.Appcode;

namespace AnyOffice.Web.general.system.netshareMager
{
    public partial class fileupload : BasePage
    {
        DiskDao bll = new DiskDao();
        public string upSize = string.Empty;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                if (Session["pid"] != null)
                {
                    labl_path.Text = bll.GetParentPath(base.Conn, Session["pid"].ToString(), "");
                }
            }
            upSize = ConfigurationManager.AppSettings["MangeruploadSize"];
        }

        protected void btnUpload_Click(object sender, EventArgs e)
        {
            int size = int.Parse(upSize) * 1024 * 1024;
            if (filup_name.PostedFile.ContentLength > size || FileUpload1.PostedFile.ContentLength > size || FileUpload2.PostedFile.ContentLength > size)
            {
                MessageBox.Show(this, "上传文件大小为" + upSize + "M 以下！");
                return;
            }
           
            string path = "";
            AnyOffice.Model.Disk fold = new AnyOffice.Model.Disk();
            if (Session["pid"] != null)
            {
                fold.ParentDiskID = Convert.ToInt32(Session["pid"].ToString());
                fold.DiskTypeID = bll.GetModel(base.Conn, Convert.ToInt32(Session["pid"].ToString())).DiskTypeID;
                path = bll.GetParentPath(base.Conn, Session["pid"].ToString(), "");
                path = Public_fold.GetPath(path, "", fold.DiskTypeID.ToString());
            }
            fold.UserID = base.User.Identity.Name;
            if (!string.Empty.Equals(filup_name.PostedFile.FileName))
            {
                string filename = filup_name.FileName.Trim().ToString();
                string kzname = filename.Substring(filename.LastIndexOf(".") + 1);//获得文件后缀名
                fold.FileTypeID = Public_fold.GetType(kzname);
                filename = Public_fold.SqlInsertEncode(filename);
                fold.Name = filename;
                fold.Keyword = txt_key.Text.Trim().ToString();
                fold.UpdateTime = DateTime.Parse(DateTime.Now.ToShortDateString());
                fold.FileSize = Decimal.Parse(filup_name.PostedFile.ContentLength.ToString()) / Decimal.Parse("1024");
                if (Upfile(path, filename,filup_name))//如果写入一条数据,上传文件，
                {
                    bll.Add(base.Conn, fold);

                }
                else
                {
                    MessageBox.Show(this, filup_name.PostedFile.FileName+"此文件已存在！");
                }
            }
            if (!string.Empty.Equals(FileUpload1.PostedFile.FileName))
            {
                string filename = FileUpload1.FileName.Trim().ToString();
                string kzname = filename.Substring(filename.LastIndexOf(".") + 1);//获得文件后缀名
                fold.FileTypeID = Public_fold.GetType(kzname);
                filename = Public_fold.SqlInsertEncode(filename);
                fold.Name = filename;
                fold.Keyword = txt_key.Text.Trim().ToString();
                fold.UpdateTime = DateTime.Parse(DateTime.Now.ToShortDateString());
                fold.FileSize = Decimal.Parse(FileUpload1.PostedFile.ContentLength.ToString()) / Decimal.Parse("1024");
                if (Upfile(path, filename,FileUpload1))//如果写入一条数据,上传文件，
                {
                    bll.Add(base.Conn, fold);

                }
                else
                {
                    MessageBox.Show(this, FileUpload1.PostedFile.FileName+"此文件已存在！");
                }
            }
            if (!string.Empty.Equals(FileUpload2.PostedFile.FileName))
            {
                string filename = FileUpload2.FileName.Trim().ToString();
                string kzname = filename.Substring(filename.LastIndexOf(".") + 1);//获得文件后缀名
                fold.FileTypeID = Public_fold.GetType(kzname);
                filename = Public_fold.SqlInsertEncode(filename);
                fold.Name = filename;
                fold.Keyword = txt_key.Text.Trim().ToString();
                fold.UpdateTime = DateTime.Parse(DateTime.Now.ToShortDateString());
                fold.FileSize = Decimal.Parse(FileUpload2.PostedFile.ContentLength.ToString()) / Decimal.Parse("1024");
                if (Upfile(path, filename,FileUpload2))//如果写入一条数据,上传文件，
                {
                    bll.Add(base.Conn, fold);

                }
                else
                {
                    MessageBox.Show(this, FileUpload2.PostedFile.FileName+"此文件已存在！");
                    
                }
            }

           

            ClientScript.RegisterStartupScript(this.GetType(), "", "<script language = javascript>parent.menu_main.location.href='public_folder.aspx?pid=" +
                   Session["pid"].ToString() + "'</script>");
        }

        /// <summary>
        ///  上传文件
        /// </summary>
        /// <param name="FolderPathName">物理路径</param>
        public bool Upfile(string FolderPathName, string name,FileUpload up)
        {
            bool isexist = false;
            try
            {
                if (up.FileName.Length > 0)
                {
                    FolderPathName = FolderPathName + "\\" + name;
                    if (File.Exists(FolderPathName))
                    { isexist = false; }
                    else
                    {
                        up.PostedFile.SaveAs(FolderPathName);
                        isexist = true;
                    }
                }
            }
            catch
            {
                return false;
            }
            return isexist;
        }
    }
}